package com.marion.zeus

import android.os.Bundle
import android.view.View
import android.webkit.WebViewClient
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.tabs.TabLayout
import com.google.gson.Gson
import com.marion.zeus.config.ConfigBase
import com.marion.zeus.config.WebId
import com.marion.zeus.databinding.ActivityMainBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext



class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    var tagData:List<ConfigBase.Item>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        val id = FieldSpec.builder(String.class,"LenovoConfig", Modifier.PUBLIC).build()
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
//        MobilePlatform.LenovoConfig
//        https://www.lenovo.com.cn/
        binding.test.webViewClient = WebViewClient()
        binding.test.settings.javaScriptEnabled = true
        GlobalScope.launch(Dispatchers.Main) {
            val bean=  withContext(Dispatchers.IO){
                Gson().fromJson(MobilePlatform.LenovoConfig,ConfigBase::class.java)
            }
            bean?.data?.modules?.forEach {
                if(it.webviewParameter!= null){
                    when(it.webviewId){
                        WebId.TITLE_VIEW->{
                            if(it.webviewParameter!!.native_title_enable){
                                binding.title.visibility = View.VISIBLE
                                binding.title.text = it.webviewParameter!!.native_title_title?:""
                            }else{
                                binding.title.visibility = View.GONE
                                binding.title.text = ""
                            }
                        }
                        WebId.NAVI_BAR->{
                            tagData = it.webviewParameter?.Items
                            if(tagData == null){
                                binding.bottomTab.visibility = View.GONE
                            }else{
                                binding.bottomTab.visibility = View.VISIBLE
                                var firstTabUrl = ""
                                tagData!!.forEachIndexed { index, item ->
                                    if(index == 0){
                                        firstTabUrl = item.Url?:""
                                    }
                                    binding.bottomTab.addTab(binding.bottomTab.newTab().setText(item.Title)
                                        .setId(index).setTag(item.Url)
                                        ,index)
                                }
                                binding.bottomTab.addOnTabSelectedListener(object :
                                    TabLayout.OnTabSelectedListener{
                                    override fun onTabSelected(tab: TabLayout.Tab?) {
                                        binding.test.loadUrl(tab!!.tag as String)
                                    }

                                    override fun onTabUnselected(tab: TabLayout.Tab?) {

                                    }

                                    override fun onTabReselected(tab: TabLayout.Tab?) {
                                        binding.test.loadUrl(tab!!.tag as String)
                                    }

                                })
                                binding.test.loadUrl(firstTabUrl)
                            }
                        }
                    }
                }
            }
        }
    }


    override fun onBackPressed() {
        if(binding.test.canGoBack()){
            binding.test.goBack()
            return
        }
        super.onBackPressed()
    }

}